package com.zxg.androidbuslearn

import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.support.v4.content.LocalBroadcastManager
import android.view.View
import android.widget.Button
import android.widget.TextView

/**
 * 作者: 赵小刚
 * 生成时间: 2018/11/5.
 * 版本: 1.0
 * 简介:
 * 修改记录:
 */
class LocalReceuverActivity: BaseActivity() {

    private var localReceiver: LoginBReceiver? = null
    private var localBroadcastManager: LocalBroadcastManager? = null
    private var intentFilter: IntentFilter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 本地广播：
        // 使用LocalBroadManager来管理广播
        //1.调用LocalBroadManager.getInstance()获得实例
        //2.调用~.registerReceive()注册广播
        //3.调用~sendBroadcast()发送广播
        //4.调用~unregisterReceiver的实例
        localBroadcastManager = LocalBroadcastManager.getInstance(this)

        // 初始化广播接收者，设置过滤器
        localReceiver = LoginBReceiver()
        intentFilter = IntentFilter()
        intentFilter!!.addAction("com.jay.mybcreceiver.LOGIN_OTHER")
        localBroadcastManager!!.registerReceiver(localReceiver!!, intentFilter!!)

        val btn_send: TextView = findViewById(R.id.mainText)
        btn_send.setOnClickListener {
            val intent = Intent("com.jay.mybcreceiver.LOGIN_OTHER")
            localBroadcastManager!!.sendBroadcast(intent)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        localBroadcastManager!!.unregisterReceiver(localReceiver!!)
    }
}